<?php


namespace App\Http\Controllers;



use App\Http\GeneratePDF\newPDF;
use App\Http\Models\Billing;
use App\Http\Models\CsvUploadFile;
use App\Http\Models\IpAcquire;
use App\Http\Models\ProductInfo;
use App\Http\Models\RedisModel;
use App\Http\Models\RequestSuccess;
use App\Http\Models\UserCompany;
use App\Jobs\ConfirmationRequestSendMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Mockery\Exception;

class ConfirmationController extends Controller
{
    function comfirmationRequest (Request $request) {
        $IpAcquire= new IpAcquire();
        $res = $IpAcquire ->ipAcquire($request);
        if (!$res) {
            return $this->errorWithInfo("現在のIPアドレスではアクセス権限がありません。エコドラフト管理者に連絡してください。");
        } else {
            $date = $request->date;
            $userCompany = new UserCompany();
            $requestCompany = $userCompany->selectRequestCompany($date);
            $requestSuccess = new RequestSuccess();
            $redisModel = new RedisModel();
            \Log::info('start'. now());
            foreach ($requestCompany as $companyInformation) {
                $billing_mail_addr = $companyInformation['billing_mail_addr'];
                if (empty($billing_mail_addr)) {
                    return $this->successWithData(false);
                }
            }
            $requestSuccess->updateFlg(1);
            \Log::info('end'. now());

            return $this->successWithData(true);
        }
    }

    function selectYearMonth (Request $request) {
        $IpAcquire= new IpAcquire();
        $res = $IpAcquire ->ipAcquire($request);
        if (!$res) {
            return $this->errorWithInfo("現在のIPアドレスではアクセス権限がありません。エコドラフト管理者に連絡してください。");
        } else {
            $requestSuccess = new RequestSuccess();
            $res = $requestSuccess->getYearMonth();
            return $this->successWithData($res);
        }
    }
    function selectRequestSuccessStatue (Request $request) {
        $IpAcquire= new IpAcquire();
        $res = $IpAcquire ->ipAcquire($request);
        if (!$res) {
            return $this->errorWithInfo("現在のIPアドレスではアクセス権限がありません。エコドラフト管理者に連絡してください。");
        } else {
            $yearMonth = $request->yearMonth;
            $requestSuccess = new RequestSuccess();
            $res = $requestSuccess->selectRequestSuccessStatue($yearMonth);
            return $this->successWithData($res);
        }
    }

    function selectTableInfor (Request $request){
        $IpAcquire= new IpAcquire();
        $res = $IpAcquire ->ipAcquire($request);
        if (!$res) {
            return $this->errorWithInfo("現在のIPアドレスではアクセス権限がありません。エコドラフト管理者に連絡してください。");
        } else {
            $chooseDate = $request->yearMonthDay;
            $currentPage = $request->currentPage;
            $pageSize = $request->pageSize;
            $yearMonth = $request->yearMonth;
            $userCompany = new UserCompany();
            $res = $userCompany->selectRequestInformation($chooseDate, $currentPage, $pageSize, $yearMonth);
            return $this->successWithData($res);
        }
    }

    function updateCsvNumber (Request $request){
        $IpAcquire= new IpAcquire();
        $res = $IpAcquire ->ipAcquire($request);
        if (!$res) {
            return $this->errorWithInfo("現在のIPアドレスではアクセス権限がありません。エコドラフト管理者に連絡してください。");
        } else {
            $billingId = $request->id;
            $csv_number = $request->csv_number;
            $total_money = $request->total_money;
            $member_price = $request->member_price;
            $billin = new Billing();
            $res = $billin->updateCsvNumber($billingId, $csv_number, $total_money,$member_price);
            return $this->successWithData($res);
        }
    }

    function getErrorPath (Request $request){
        $IpAcquire= new IpAcquire();
        $res = $IpAcquire ->ipAcquire($request);
        if (!$res) {
            return $this->errorWithInfo("現在のIPアドレスではアクセス権限がありません。エコドラフト管理者に連絡してください。");
        } else {
            $yearMonth = $request->yearMonth;
            $flg = $request->flg;
            $csvUploadFile = new CsvUploadFile();
            $res = $csvUploadFile->getErrorPath($yearMonth, $flg);
            if (!empty($res)) {
                return $this->successWithData($res);
            }/* else {
                return $this->errorWithInfo(false);
            }*/
        }
    }

}
